package com.ymf.page.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ymf.page.mapper.DescMapper;
import com.ymf.page.mapper.MyMapper;
import com.ymf.page.mapper.SkillsMapper;
import com.ymf.page.pojo.Description;
import com.ymf.page.pojo.MyMessage;
import com.ymf.page.pojo.Skills;
import com.ymf.page.pojo.data;
import com.ymf.page.util.CacheKey;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class MyService {
    @Autowired
    @SuppressWarnings("all")
    private MyMapper myMapper;
    @Autowired
    @SuppressWarnings("all")
    private DescMapper descMapper;
    @Autowired
    @SuppressWarnings("all")
    private SkillsMapper skillsMapper;

    public List<MyMessage> findAll(){
        return myMapper.selectList(null);
    }
    public List<Description> findDescription(int description_id){
        return myMapper.findDesc(description_id);
    }
    public int updataAboutMe(MyMessage about){
        //设置条件
        QueryWrapper<MyMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("id",1);
        return myMapper.update(about,wrapper);
    }
    public int updataMy_message(MyMessage myMessage){
        //设置条件
        QueryWrapper<MyMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("id",1);
        return myMapper.update(myMessage,wrapper);
    }
    //查询技能表
    public List<Skills> findallSkills(){
        return skillsMapper.selectList(null);
    }
    //根据id删除技能
    public int deleteSkillsById(int id){
        return skillsMapper.deleteById(id);
    }
    //插入技能表数据
    public int insertSkills(Skills skills){
        return skillsMapper.insert(skills);
    }
    //修改技能表数据
    public int updataSkills(Skills skills){
        return skillsMapper.updateById(skills);
    }
    //修改个性签名
    public int updataSignature(MyMessage myMessage){
        QueryWrapper<MyMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("id",1);
        return myMapper.update(myMessage,wrapper);
    }
    //删除标签
    public int deleteEscription(Description description){
        QueryWrapper<Description> wrapper = new QueryWrapper<>();
        wrapper.eq("data",description.getData());
        return descMapper.delete(wrapper);
    }
    //添加标签
    public int insertDescription(Description description){
        return descMapper.insert(description);
    }
    //修改公告
    public int updateNotice(MyMessage myMessage){
        QueryWrapper<MyMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("id",1);
        int update = myMapper.update(myMessage, wrapper);
        saveNoticeRedis();//修改数据后重新进行缓存预热
        return update;
    }
    //修改头像
    public int uodateAvatar(MyMessage myMessage){
        QueryWrapper<MyMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("id",1);
        System.out.println(myMessage.getAbout());
        return myMapper.update(myMessage,wrapper);
    }


    //redis缓存
    @Autowired
    private RedisTemplate redisTemplate;

    public void saveNoticeRedis(){
        List<MyMessage> myMessages = myMapper.selectList(null);
        String notice = myMessages.get(0).getNotice();

        redisTemplate.boundValueOps(CacheKey.notice).set(notice);
    }

    public Object test(){
        return redisTemplate.boundValueOps(CacheKey.notice).get();
    }
}
